package contegor.scire.core;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class StorageResultsReturnPoint<T> extends ReturnPointAdapter<T> {

    public List<T> results = null;
    public List<Exception> exceptions = null;

    public <U extends T> void manage(TaskId taskId, U result) {
      if (results == null) {
        synchronized(this) {
          if (results == null)
            results = Collections.synchronizedList(new ArrayList<T>());
        }
      }
      results.add(result);
    }

    public <U extends T> void manage(TaskId taskId, Collection<U> result) {
      if (results == null) {
        synchronized(this) {
          if (results == null)
            results = Collections.synchronizedList(new ArrayList<T>());
        }
      }
      results.addAll(result);
    }

    public void manage(TaskId taskId, Exception exception) {
      super.manage(taskId, exception);
      if (exceptions == null) {
        synchronized(this) {
          if (exceptions == null)
            exceptions = Collections.synchronizedList(new ArrayList<Exception>());
        }
      }
      exceptions.add(exception);
    }
  }